package com.tengfei.main.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tengfei.common.core.domain.PageQuery;
import com.tengfei.common.core.domain.R;
import com.tengfei.common.core.page.TableDataInfo;
import com.tengfei.common.exception.ServiceException;
import com.tengfei.common.helper.LoginHelper;
import com.tengfei.main.question.domain.*;
import com.tengfei.main.question.mapper.*;
import com.tengfei.main.user.domain.SearchEntity;
import com.tengfei.main.user.domain.bo.quBank.UserQuAnsBo;
import com.tengfei.main.user.domain.vo.quBank.*;
import com.tengfei.main.user.service.IUserQuBankService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author feige
 * @version v1.0
 * @date 2022-08-15-21:30
 * @description
 */

@Service
public class UserQuBankServiceImpl implements IUserQuBankService {

    @Resource
    private QuestionBankMapper questionBankMapper;
    @Resource
    private ObjectiveMapper objectiveMapper;
    @Resource
    private SubjectiveMapper subjectiveMapper;
    @Resource
    private QbQuMapper qbQuMapper;
    @Resource
    private QbUserMapper qbUserMapper;
    @Resource
    private QbUserRecordMapper qbUserRecordMapper;

    /**
     * 题库列表
     *
     * @param search
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<UserQuBankVo> quBankList(SearchEntity search, PageQuery pageQuery) {
        Page<UserQuBankVo> userQuBankVoPage = questionBankMapper.selectQuBankList(search, pageQuery.build());
        return TableDataInfo.build(userQuBankVoPage);
    }

    /**
     * 根据id查询客观题目详细信息(含答案)
     *
     * @param objId
     * @return
     */
    @Override
    public UserObjVo objQuById(Long objId) {
        Objective objective = objectiveMapper.selectById(objId);
        if (ObjectUtil.isNull(objective)) {
            return null;
        }
        UserObjVo userObjVo = BeanUtil.copyProperties(objective, UserObjVo.class);
        return userObjVo;
    }


    /**
     * 根据id查询客观题目详细信息(含答案)
     *
     * @param subId
     * @return
     */
    @Override
    public UserSubVo subQuById(Long subId) {
        Subjective subjective = subjectiveMapper.selectById(subId);
        if (ObjectUtil.isNull(subjective)) {
            return null;
        }
        UserSubVo userSubVo = BeanUtil.copyProperties(subjective, UserSubVo.class);
        return userSubVo;
    }

    /**
     * 上传题目答案
     *
     * @param bo
     * @return
     */
    @Override
    public boolean uploadQuAns(UserQuAnsBo bo) {
        QbUserRecord qbUserRecord = BeanUtil.copyProperties(bo, QbUserRecord.class);
        // 获取用户信息
        Long userId = LoginHelper.getUserId();
        qbUserRecord.setUserId(userId);
        // 查询答案是否存在
        if (ObjectUtil.isNotNull(qbUserRecord.getId()) &&
                ObjectUtil.isNotNull(qbUserRecordMapper.selectById(qbUserRecord.getId()))
        ) {
            // 更新答案
            return qbUserRecordMapper.updateById(qbUserRecord) > 0;
        }
        // 重置id为null
        qbUserRecord.setId(null);
        // 上传答案
        return qbUserRecordMapper.insert(qbUserRecord) > 0;
    }

    /**
     * 获取用户已答题目的答案信息
     *
     * @param id
     * @return
     */
    @Override
    public UserQuAnsVo getUserQuAns(Long id) {
        QbUserRecord qbUserRecord = qbUserRecordMapper.selectById(id);
        if (ObjectUtil.isNull(qbUserRecord)) {
            return null;
        }
        UserQuAnsVo userQuAnsVo = BeanUtil.copyProperties(qbUserRecord, UserQuAnsVo.class);
        return userQuAnsVo;
    }

    /**
     * 查询用户题库已答题目答案列表信息
     *
     * @param qbId
     * @return
     */
    @Override
    public List<UserQuAnsVo> getUserQuAnsList(Long qbId) {
        Long userId = LoginHelper.getUserId();
        List<QbUserRecord> qbUserRecords = qbUserRecordMapper.selectList(new LambdaQueryWrapper<QbUserRecord>()
                .eq(QbUserRecord::getQbId, qbId)
                .eq(QbUserRecord::getUserId, userId)
                .orderByAsc(QbUserRecord::getQuType));
        if (ObjectUtil.isNull(qbUserRecords)) {
            return null;
        }
        List<UserQuAnsVo> userQuAnsVos = BeanUtil.copyToList(qbUserRecords, UserQuAnsVo.class);
        return userQuAnsVos;
    }


    /**
     * 查询题库题目列表
     *
     * @param qbId
     * @return
     */
    @Override
    public List<UserQbQuVo> qbQuList(Long qbId) {
        if (ObjectUtil.isNull(qbId)) {
            throw new ServiceException("题库id不能为空");
        }
        List<QbQu> qbQus = qbQuMapper.selectList(new LambdaQueryWrapper<QbQu>()
                .eq(QbQu::getQbId, qbId)
                .orderByAsc(QbQu::getQuType));
        List<UserQbQuVo> userQbQuVos = BeanUtil.copyToList(qbQus, UserQbQuVo.class);
        return userQbQuVos;
    }

    /**
     * 查询题目详细信息
     */
    @Override
    public R<Object> quInfo(Long quId, Integer quType) {
        if (isObj(quType)) {
            // 客观题
            Objective objective = objectiveMapper.selectById(quId);
            if (ObjectUtil.isNull(objective)) {
                return R.fail("题目不存在");
            }
            UserObjVo userObjVo = BeanUtil.copyProperties(objective, UserObjVo.class);
            return R.ok(userObjVo);
        } else {
            // 主观题
            Subjective subjective = subjectiveMapper.selectById(quId);
            if (ObjectUtil.isNull(subjective)) {
                return R.fail("题目不存在");
            }
            UserSubVo userSubVo = BeanUtil.copyProperties(subjective, UserSubVo.class);
            return R.ok(userSubVo);
        }
    }

    /**
     * 查询用户个人题库列表
     */
    @Override
    public List<UserQuBankVo> userQuBankList() {
        // 获取用信息
        Long userId = LoginHelper.getUserId();
        List<Long> qbIds = qbUserMapper.selectList(new LambdaQueryWrapper<QbUser>()
                .eq(QbUser::getUserId, userId))
                .stream()
                .map(item -> item.getQbId())
                .collect(Collectors.toList());
        if (ObjectUtil.isNull(qbIds)||qbIds.size()<=0) {
            return new ArrayList<>(0);
        }
        List<QuestionBank> questionBanks = questionBankMapper.selectList(new LambdaQueryWrapper<QuestionBank>()
                .in(QuestionBank::getQbId, qbIds));
        List<UserQuBankVo> userQuBankVos = BeanUtil.copyToList(questionBanks, UserQuBankVo.class);
        return userQuBankVos;
    }

    // 非空判断+界限判断+是否为客观题
    private boolean isObj(Integer quType) {
        if (quType.intValue() < 1 || quType.intValue() > 5) {
            throw new ServiceException("题目类型不符合规范");
        } else if (quType.intValue() >= 1 && quType.intValue() <= 3) {
            return true;
        } else {
            return false;
        }
    }
}
